package com.sy.controller;


import com.sy.bean.GoodsList;
import com.sy.bean.SaveGoods;
import com.sy.bean.pucharse.Condition.GoodListCondition;
import com.sy.bean.pucharse.excel.GoodsExcel;
import com.sy.common.bean.PageBean;
import com.sy.common.bean.ResultBean;
import com.sy.common.controller.BaseController;
import com.sy.common.exception.ValidateException;
import com.sy.common.utils.BeanOfMap;
import com.sy.common.utils.UploadUtils;
import com.sy.mapper.GoodsListConditionMapper;
import com.sy.mapper.SaveGoodsMapper;
import com.sy.service.GoodsListConditionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/set")
@Api(tags = "商品列表管理")
public class GoodsListConditionController extends BaseController {

    @Autowired
    GoodsListConditionService goodsListConditionService;

    @Autowired
    GoodsListConditionMapper goodsListConditionMapper;

    @Autowired
    SaveGoodsMapper saveGoodsMapper;



    /**
     * 商品列表查询
     * @param goodListCondition
     * @return
     */
    @ApiOperation("商品列表")
    @PostMapping("/goods/list")
    public ResultBean<GoodsList> GoodQuery(@RequestBody GoodListCondition goodListCondition){
        PageBean pageBean = goodsListConditionService.query(BeanOfMap.bom(goodListCondition));
        return success("商品列表",pageBean);
    }

    /**
     * 导出商品列表excel
     */
    @ApiOperation("导出商品列表")
    @PostMapping("/goods/list/export")
    public ResultBean<GoodsExcel> goodsExport(@RequestBody Map map){
        String url = goodsListConditionService.goodsExport(map);
        return success("导出商品列表",url);
    }


    /**
     * 导入商品excel列表
     */
    @ApiOperation("导入excel")
    @PostMapping("/goods/list/import")
    public ResultBean importGoodsExcel(MultipartFile file){
        int rs = goodsListConditionService.readExcel(file);
        return rs>0?success("导入成功"):fail("导入失败");
    }

    /**
     * 批量启用
     * @param ids
     * @return
     */
    @ApiOperation("启用商品")
    @PutMapping("/goods/enable")
    public ResultBean batchUpdate(@RequestParam("ids") List<Integer> ids){
        int rs = goodsListConditionMapper.batchUpdate(ids);
        return rs>0?success("启用成功！"):fail("启用失败！");
    }


    /**
     * 删除
     */
    @ApiOperation("删除商品")
    @PutMapping("goods/delete")
    public ResultBean deleteGoods(@RequestParam("id") int id){
        int rs = goodsListConditionMapper.delete(id);
        return rs==1?success("删除成功！"):fail("删除失败！");
    }

    /**
     * 根据商品id查询商品详情
     */
    @ApiOperation("根据商品id查询商品详情")
    @GetMapping("goods/getById")
    public ResultBean get(@RequestParam("id") int id){
        SaveGoods saveGoods = saveGoodsMapper.get(id);
        return success("商品详情",saveGoods);
    }


    /**
     * 修改商品信息
     * @param saveGoods
     * @return
     */
    @ApiOperation("修改商品信息")
    @PutMapping("goods/updateGoods")
    public ResultBean updateGoods(@RequestBody SaveGoods saveGoods){
        if (null == saveGoods.getWarehouseGoodsList() || saveGoods.getGoodsWarehouseList().size() == 0){
            throw new ValidateException("WarehouseGoodsList","采购明细不能为空");
        }
        int rs =saveGoodsMapper.update(saveGoods);
        return gotoResult(rs,"修改");
    }


}
